#include "ecggridwidget.h"
#include <PXUtils.h>
#include <QPainter>
EcgGridWidget::EcgGridWidget(QWidget *parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_NoSystemBackground);
    setAutoFillBackground(false);
    //获取设备像素比
    auto CmToPx =  PXUtils::calcCmToPx(); //1cm = px
    CmToPxValue = CmToPx.first; // 水平像素密度
}
void EcgGridWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    int w = width();
    int h = height();


    double smallStep = CmToPxValue * 0.5; // 小格尺寸
    // 绘制背景
    painter.fillRect(rect(), QColor(255, 255, 255));


    //画小点，每隔1mm
    painter.setPen(QPen(QColor(153,153,153), 0.8));
    double dotStep = CmToPxValue * 0.1; // 1mm
    for (double x = 0; x < w; x += dotStep){
        for (double y = 0; y < h; y += dotStep){
            painter.drawPoint(x, y);
        }
    }

    // 小格线条（浅红） 大格深红
    int xIndex = 0;
    for (double x = 0; x < w; x += smallStep){
        if( xIndex % 2 == 0 ) painter.setPen(QPen(QColor(255, 80, 80), 1,Qt::SolidLine));
        else painter.setPen(QPen(QColor(255, 180, 180), 1, Qt::DashLine));
        painter.drawLine(x, 0, x, h);
        xIndex++;
    }
    int yIndex = 0;
    for (double y = 0; y < h; y += smallStep){
        if( yIndex % 2 == 0 )  painter.setPen(QPen(QColor(255, 80, 80), 1,Qt::SolidLine));
        else painter.setPen(QPen(QColor(255, 180, 180), 1, Qt::DashLine));
        painter.drawLine(0, y, w, y);
        yIndex++;
    }


}
